package com.voute.Presenter.Impl;


import com.voute.Model.Bean.BaseBean;
import com.voute.Model.Bean.HotWordsBean;
import com.voute.Model.Bean.SearchResultBean;
import com.voute.Model.ISearchModel;
import com.voute.Model.Impl.SearchImpl;
import com.voute.Presenter.ISearchPresenter;
import com.voute.Utils.CommonSubscriber;
import com.voute.Utils.TransformUtils;
import com.voute.View.IView.ISearchView;

import java.util.HashMap;

/**
 * 资源详情Presenter实现类
 * Created by liukanshan on 2016/12/23.
 */

public class SearchPresenterImpl implements ISearchPresenter {

    private ISearchView search_view;
    private ISearchModel model;

    public SearchPresenterImpl(ISearchView view) {
        if (view != null) {
            search_view = view;
            model = new SearchImpl();
        }
    }

    @Override
    public void getHotWords() {
        search_view.showLoading();
        model.getHotWords().compose(TransformUtils.<HotWordsBean>defaultSchedulers()).subscribe(new CommonSubscriber<HotWordsBean>() {
            @Override
            public void onSuccess(HotWordsBean response) {
                search_view.hideLoading();
                search_view.setHotWords(response);
            }

            @Override
            public void onFailure(BaseBean response) {
                search_view.hideLoading();
                search_view.showError(response.getMsg());
            }
        });
    }

    @Override
    public void getSearchResults(HashMap<String, String> values) {
        search_view.showLoading();
        model.getSearchResults(values).compose(TransformUtils.<SearchResultBean>defaultSchedulers()).subscribe(new CommonSubscriber<SearchResultBean>() {
            @Override
            public void onSuccess(SearchResultBean response) {
                search_view.setSearchResults(response);
                search_view.hideLoading();
            }

            @Override
            public void onFailure(BaseBean response) {
                search_view.showError(response.getMsg());
                search_view.hideLoading();
            }
        });
    }

    @Override
    public void getRecommandSearch(String q) {
        model.getRecommandSearch(q).compose(TransformUtils.<SearchResultBean>defaultSchedulers()).subscribe(new CommonSubscriber<SearchResultBean>() {
            @Override
            public void onSuccess(SearchResultBean response) {
                search_view.setRecommandSearch(response);
            }

            @Override
            public void onFailure(BaseBean response) {
                search_view.showError(response.getMsg());
            }
        });
    }
}
